package com.enki.cigarettes.repository;

import com.enki.cigarettes.entity.ProductCommentT;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * @author WCS.Wang
 * @version V1.0
 * @Package com.enki.cigarettes.repository
 * @Name e-cigarettes-shop-api-module
 * @Description: TODO 商品评论数据层
 * @date 2019-07-19
 */
public interface ProductCommentTRepository extends PagingAndSortingRepository<ProductCommentT, String> {

    // 分页获取商品评论信息
    Page<ProductCommentT> findByProductTId(long productTId, Pageable pageable);

    ProductCommentT findOneById(String id);

    // 订单ID、订单类目ID、商品ID、商品skuID
    ProductCommentT findOneByOrderTIdAndOrderItemTIdAndProductTIdAndProductSkuTId(String orderTId, String orderItemTId, long productTId, String productSkuTId);

    /**
     * 商品评论数量
     *
     * @param productId 商品ID
     * @return
     */
    Long countByProductTId(Long productId);

    /**
     * 根据评分拿评论数量
     *
     * @param productId
     * @param scoreLess
     * @return
     */
    long countByProductTIdAndScoreGreaterThanEqual(Long productId, Integer scoreLess);

    /**
     * 评论表分产品统计评论数
     *
     * @return
     */
    @Query(value = "SELECT pcomment.product_t_id,count(pcomment.id) FROM hs_aec_store.product_comment_t as pcomment group by product_t_id;", nativeQuery = true)
    List<Object[]> getCountCommendGroupByProductId();

    /**
     * 查询最新五条评论信息
     */
    List<ProductCommentT> findTop5ByProductTId(Long pid, Sort sort);


}
